﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using System.Drawing;
using System.Web.Services;


public partial class JOINT_frmJ_StaffEnterInput : System.Web.UI.Page
{
    private 入退社情報 obj = new 入退社情報();

    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("入退社情報入力");
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_StaffEnterInput");
        if (Session["役"] == null || Session["役"].ToString() == "")
        {
            Response.Redirect("~/frmD_Login.aspx");
        }
        if (!IsPostBack)
        {
            ((JOINT_MstPage_JOINT)this.Master).changeTitle("入退社情報登録");
            if (Session["ModeUpdateStaffEnterInput"] != null)// hien thi giao dien update
            {
                if (Session["IDStaffBasicInfoInput"] != null && Session["ModeUpdateStaffEnterInput"].Equals("Update"))
                {
                    btn_登録.Enabled = false;
                    int ID = Convert.ToInt32(Session["IDStaffBasicInfoInput"].ToString());
                        Session.Add("IDpaging", ID);// gan id cua nhan vien  de  load lai gird view sau nay
                        showGrid(ID);
                    
                    checkUpdate();
                }
                // hien thi gridv insert 
                else if (Session["ModeUpdateStaffEnterInput"].Equals("Insert") && Session["IdEmp"] != null)
                {
                    Int32 社員ID = Convert.ToInt32(Session["IdEmp"].ToString());
                    Session.Add("IDpaging", 社員ID);
                    // Session["IDpaging"] = 社員ID;
                    showGrid(社員ID);
                }
            }
            
            else if (Session["IdEmp"] == null || Session["ModeUpdateStaffEnterInput"] != null)
            {
                Response.Redirect("frmJ_StaffBasicInfoInput.aspx");
            }
            ddl採用区分.Focus();
        }
        RequiredFieldValidator1.ErrorMessage = GetError("ER00007");
        RequiredFieldValidator22.ErrorMessage = GetError("ER00001");
        //   RequiredFieldValidator3.ErrorMessage = "退社年月日 " + GetError("ER00001");
        CustomValidator3.ErrorMessage = GetError("ER00007");
        CustomValidator1.ErrorMessage = GetError("ER00005");
        CustomValidator6.ErrorMessage = GetError("ER00006");
        // CustomValidator7.ErrorMessage = "退社年月日 " + GetError("ER00005");
        CustomValidatorCheckDate1.ErrorMessage = GetError("ER00006");
    }
    protected void btn_登録_click(object sender, EventArgs e)
    {
        //tuy theo mode ma su ly update hay insert

        // obj.採用区分 = null;
        obj.廃止区分 = 0;
        if (ddl採用区分.SelectedValue.ToString() != "-1")
        {
            obj.採用区分 = Convert.ToInt32(ddl採用区分.SelectedValue.ToString());
        }
        try
        {
            if (txt入社年月日.Text.ToString() != "")
            {
                DateTime d1;
                d1 = Convert.ToDateTime(txt入社年月日.Text);
                obj.入社年月日 = d1;
            }
        }
        catch (Exception ex)
        {

        }
        obj.入社特記事項 = HttpUtility.HtmlEncode(txt入社特記事項.Text.Trim());
        try
        {
            if (txt退社年月日.Text.ToString() != "")
            {
                DateTime d1;
                d1 = Convert.ToDateTime(txt退社年月日.Text);
                obj.退社年月日 = d1;
            }
        }
        catch (Exception ex) { }
        obj.退社特記事項 = HttpUtility.HtmlDecode(txt退社特記事項.Text.Trim());
        if (ddl退職理由.SelectedValue.ToString() == "-1")
        {
            //  obj.退職理由1=null;
        }
        if (ddl退職理由.SelectedValue.ToString() != "-1")
        {
            obj.退職理由1 = Convert.ToInt32(ddl退職理由.SelectedValue.ToString());
        }
        入退社情報Manager manager = new 入退社情報Manager();
        if (Session["ModeUpdateStaffEnterInput"] != null && (Session["ModeUpdateStaffEnterInput"].Equals("Update") && btn_登録.Text.ToString().Equals("更新")))
        {
            int Id = Convert.ToInt32(ViewState["CurrentID"].ToString());
            obj.ＩＤ = Id;
            manager.Update入退社情報(obj);
            Response.Redirect("frmJ_EmpBasicInfo.aspx");
        }
        if (ViewState["CurrentID"] != null && btn_登録.Text.ToString().Equals("更新"))
        {
            int Id = Convert.ToInt32(ViewState["CurrentID"].ToString());
            obj.ＩＤ = Id;
            manager.Update入退社情報(obj);
            if (Session["ModeUpdateStaffEnterInput"].Equals("Update"))
            {
                Response.Redirect("frmJ_EmpBasicInfo.aspx");
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "run", "clearText();", true);

                Int32 社員ID = Convert.ToInt32(Session["IdEmp"].ToString());
                showGrid(社員ID);
                btn_登録.Text = "登録";
                Response.Redirect("frmJ_StaffEnterInput.aspx");
                return;
            }
            ViewState["CurrentID"] = null;
        }
        if (Session["IdEmp"] != null && Session["ModeUpdateStaffEnterInput"].Equals("Insert") && btn_登録.Text.ToString().Equals("登録"))
        {
            {
                Int32 社員ID = Convert.ToInt32(Session["IdEmp"].ToString());
                obj.社員ID = 社員ID;
                manager.Insert入退社情報(obj);
                showGrid(社員ID);
                Response.Redirect("frmJ_StaffEnterInput.aspx");
                //  ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "run", "clearText();", true);
            }
        }
        //  ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "run", "clearText();", true);

    }

    protected void btn_戻る_Click(object sender, EventArgs e)
    {

        if (Session["ModeUpdateStaffEnterInput"] != null && Session["ModeUpdateStaffEnterInput"].Equals("Update"))
        {
            Response.Redirect("frmJ_EmpBasicInfo.aspx");
        }
        else
        {
            Response.Redirect("frmJ_StaffBasicInfoInput.aspx");
        }
    }

    protected void linkClick(object sender, EventArgs e)
    { }

    protected void btn_次_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_StaffContractTypeInput.aspx");
    }
    public void showGrid(Int32 社員ID1)
    {
        int index2 = -1;
        try
        {
            if (社員ID1 != null)
            {
                入退社情報Manager manager = new 入退社情報Manager();
                DataTable table = manager.Get入退社情報By社員ID(社員ID1);
                DataColumn objDataColumn1 = new DataColumn("colum1採用区分", typeof(string));// add them cot 
                //de hien thi text 採用区分 trong gridview
                table.Columns.Add(objDataColumn1);
                DataColumn objDataColumn2 = new DataColumn("colum4退職理由", typeof(string));
                // add them cot 
                //de hien thi text 退職理由 trong gridview
                table.Columns.Add(objDataColumn2);
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    int index1 = Convert.ToInt32(table.Rows[i]["採用区分"].ToString());
                    if (table.Rows[i]["退職理由"].ToString() != "")
                    {
                        index2 = Convert.ToInt32(table.Rows[i]["退職理由"].ToString());
                    }
                    String 採用区分 = "";
                    String 退職理由 = "";
                    switch (index1)
                    {
                        case 0:
                            採用区分 = "即戦力（途中）";
                            break;

                        case 1:
                            採用区分 = "新規";
                            break;
                        default:

                            採用区分 = "";
                            break;
                    }
                    table.Rows[i]["colum1採用区分"] = 採用区分;

                    switch (index2)
                    {
                        case 0:
                            退職理由 = "定年";
                            break;

                        case 1:
                            退職理由 = "契約満了";
                            break;
                        case 2:
                            退職理由 = "自己都合";
                            break;

                        case 3:
                            退職理由 = "会社都合";
                            break;
                        case 4:
                            退職理由 = "失踪";
                            break;

                        case 5:
                            退職理由 = "死亡";
                            break;
                        case 6:
                            退職理由 = "その他";
                            break;
                        default:

                            退職理由 = "";
                            break;
                    }
                    table.Rows[i]["colum4退職理由"] = 退職理由;
                }
                if (table.Rows.Count > 0)
                {
                    grv.DataSource = table;
                    grv.DataBind();
                }
                else if (table.Rows.Count <= 0)
                {
                    AppCode_Common.ShowEmtyGrid<入退社情報>(grv, 4, "データがありません。", "");
                }
            }
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    public void checkUpdate()
    {
        //check mode update

        入退社情報 obj = new 入退社情報();
        if (Session["ModeUpdateStaffEnterInput"].Equals("Update") && Session["IDStaffEnterInput"] != null)
        {

            btn_登録.Text = "更新";

            入退社情報Manager manager = new 入退社情報Manager();
            obj.ＩＤ = Convert.ToInt32(Session["IDStaffBasicInfoInput"].ToString()); ;
            DataTable table = manager.Get入退社情報ByID(obj.ＩＤ);
            foreach (DataRow row in table.Rows)
            {
                ddl採用区分.Items.Remove("-1");
                ddl採用区分.SelectedValue = row["採用区分"].ToString();
                if (row["入社年月日"].ToString() != null)
                {
                    string 入社年月日 = row["入社年月日"].ToString();
                    DateTime d2 = DateTime.Parse(入社年月日);
                    入社年月日 = d2.ToString("yyyy/MM/dd");
                    txt入社年月日.Text = 入社年月日;
                }
                txt入社特記事項.Text = row["入社特記事項"].ToString();
                if (row["退社年月日"].ToString() != null)
                {
                    string 退社年月日 = row["退社年月日"].ToString();
                    if (退社年月日 != "")
                    {
                        DateTime d3 = DateTime.Parse(退社年月日);
                        退社年月日 = d3.ToString("yyyy/MM/dd");
                        txt退社年月日.Text = 退社年月日;
                    }
                }
                txt退社特記事項.Text = row["退社特記事項"].ToString();
                ddl退職理由.Items.Remove("-1");

                if (row["退職理由"].ToString() == null)
                {
                    ddl退職理由.SelectedValue = "-1";
                }
                else
                {
                    ddl退職理由.SelectedValue = row["退職理由"].ToString();
                }
            }

        }
    }
    protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //su ly hien thi du lieu len control khi  double click vao gridview insert du lieu
        try
        {
            if (e.CommandName.Equals("doubleClick"))
            {
                btn_登録.Enabled = true;
                string ID = grv.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
                
                入退社情報Manager manager = new 入退社情報Manager();
                DataTable table = manager.Get入退社情報ByID(Convert.ToInt32(ID));
                if (table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        ddl採用区分.Items.Remove("-1");
                        ddl採用区分.SelectedValue = row["採用区分"].ToString();
                        string 入社年月日 = row["入社年月日"].ToString();
                        if (入社年月日 != "")
                        {
                            DateTime d2 = DateTime.Parse(入社年月日);
                            入社年月日 = d2.ToString("yyyy/MM/dd");
                            txt入社年月日.Text = 入社年月日;
                        }
                        txt入社特記事項.Text = row["入社特記事項"].ToString();
                        string 退社年月日 = row["退社年月日"].ToString();
                        if (退社年月日 != "")
                        {
                            DateTime d3 = DateTime.Parse(退社年月日);
                            退社年月日 = d3.ToString("yyyy/MM/dd");
                            txt退社年月日.Text = 退社年月日;
                        }
                        else
                        {
                            txt退社年月日.Text = "";
                        }
                        txt退社特記事項.Text = row["退社特記事項"].ToString();
                        ddl退職理由.Items.Remove("-1");
                        if (row["退職理由"].ToString() == null)
                        {
                            ddl退職理由.SelectedValue = "-1";
                        }
                        else
                        {
                            ddl退職理由.SelectedValue = row["退職理由"].ToString();
                        }
                    }
                }
                int index = Convert.ToInt32(ID);
                ViewState["CurrentID"] = index;
                btn_登録.Text = "更新";
                Page.Header.Title = "入退社情報";
            }
        }
        catch { }
    }
    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)// phan trang.
    {
        int 社員ID = Convert.ToInt32(Session["IDpaging"].ToString());
        grv.PageIndex = e.NewPageIndex;
        this.showGrid(社員ID);
    }
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
        {
            return error = dt.Rows[0][2].ToString();
        }
        else
        {
            return error;
        }
    }
    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_StaffEnterInput.aspx");
        showGrid(Convert.ToInt32(Session["IDpaging"].ToString()));
    }
    protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("ondblclick", "javascript:__doPostBack('ctl00$ContentPlaceHolder1$grv','doubleClick$0" + e.Row.RowIndex + "')");
    }
}
